<template>
  <el-dialog
      v-model="visible"
      class="delivery-dialog-wrapper"
      title="订单发货"
      width="600px"
      @close="handleClose"
  >
    <div v-if="order" class="delivery-dialog">
      <!-- 订单信息 -->
      <div class="order-info">
        <h4>订单信息</h4>
        <div class="info-item">
          <span class="label">订单号：</span>
          <span class="value">{{ order.orderNo }}</span>
        </div>
        <div class="info-item">
          <span class="label">订单金额：</span>
          <span class="value price">¥{{ order.totalAmount }}</span>
        </div>
        <div class="info-item">
          <span class="label">收货人：</span>
          <span class="value">{{ order.receiverName }}</span>
        </div>
        <div class="info-item">
          <span class="label">收货电话：</span>
          <span class="value">{{ order.receiverPhone }}</span>
        </div>
        <div class="info-item">
          <span class="label">收货地址：</span>
          <span class="value">{{ order.receiverAddress }}</span>
        </div>
      </div>

      <!-- 发货确认 -->
      <div class="delivery-confirm">
        <el-alert
            :closable="false"
            show-icon
            title="发货确认"
            type="warning"
        >
          <template #default>
            <p>确认要对此订单进行发货操作吗？</p>
            <p>发货后订单状态将变更为"待收货"，请确保商品已准备完毕。</p>
          </template>
        </el-alert>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
            :loading="loading"
            type="primary"
            @click="handleConfirm"
        >
          确认发货
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {computed, ref} from 'vue'
import {ElMessage} from 'element-plus'
import {useOrderStore} from '@/store/order'

// Props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  order: {
    type: Object,
    default: null
  }
})

// Emits
const emit = defineEmits(['update:modelValue', 'success'])

// Store
const orderStore = useOrderStore()

// 响应式数据
const loading = ref(false)

// 计算属性
const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 方法
const handleClose = () => {
  visible.value = false
}

const handleConfirm = async () => {
  if (!props.order) return

  try {
    loading.value = true

    // 调用发货API，不需要额外参数
    await orderStore.deliverOrder(props.order.id, {})

    ElMessage.success('发货成功')
    emit('success')
    handleClose()
  } catch (error) {
    console.error('发货失败:', error)
    ElMessage.error('发货失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* 对话框样式 */
:deep(.delivery-dialog-wrapper) {
  border-radius: var(--admin-radius-lg);
  overflow: hidden;
}

:deep(.delivery-dialog-wrapper .el-dialog__header) {
  background: var(--admin-bg-secondary);
  border-bottom: 1px solid var(--admin-border-light);
  padding: var(--admin-space-lg);
}

:deep(.delivery-dialog-wrapper .el-dialog__title) {
  font-size: var(--admin-font-lg);
  font-weight: var(--admin-font-semibold);
  color: var(--admin-text-primary);
}

:deep(.delivery-dialog-wrapper .el-dialog__body) {
  padding: var(--admin-space-xl);
}

:deep(.delivery-dialog-wrapper .el-dialog__footer) {
  background: var(--admin-bg-secondary);
  border-top: 1px solid var(--admin-border-light);
  padding: var(--admin-space-lg);
}

.delivery-dialog {
  min-height: 300px;
}

.order-info {
  margin-bottom: var(--admin-space-xl);
  padding: var(--admin-space-lg);
  background: linear-gradient(135deg, var(--admin-bg-secondary), var(--admin-bg-tertiary));
  border-radius: var(--admin-radius-lg);
  border: 1px solid var(--admin-border-light);
  position: relative;
  overflow: hidden;
}

.order-info::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, var(--admin-primary), var(--admin-primary-light));
}

.order-info h4 {
  margin: 0 0 var(--admin-space-lg) 0;
  color: var(--admin-text-primary);
  font-size: var(--admin-font-md);
  font-weight: var(--admin-font-semibold);
  line-height: var(--admin-line-height-sm);
}

.info-item {
  display: flex;
  margin-bottom: var(--admin-space-md);
  align-items: center;
}

.info-item:last-child {
  margin-bottom: 0;
}

.label {
  width: 100px;
  color: var(--admin-text-secondary);
  font-size: var(--admin-font-sm);
  font-weight: var(--admin-font-medium);
  flex-shrink: 0;
}

.value {
  flex: 1;
  color: var(--admin-text-primary);
  font-size: var(--admin-font-sm);
  font-weight: var(--admin-font-medium);
}

.price {
  color: var(--admin-warning);
  font-weight: var(--admin-font-semibold);
}

.delivery-confirm {
  margin-bottom: var(--admin-space-lg);
}

.delivery-confirm :deep(.el-alert) {
  border-radius: var(--admin-radius-md);
  border: 1px solid var(--admin-warning-light);
}

.delivery-confirm :deep(.el-alert__content) {
  line-height: var(--admin-line-height-md);
}

.delivery-confirm p {
  margin: var(--admin-space-xs) 0;
  color: var(--admin-text-secondary);
  font-size: var(--admin-font-sm);
}

.delivery-confirm p:last-child {
  margin-bottom: 0;
}

.dialog-footer {
  text-align: center;
  display: flex;
  justify-content: center;
  gap: var(--admin-space-md);
}

.dialog-footer .el-button {
  min-width: 120px;
  font-weight: var(--admin-font-medium);
}

/* 响应式设计 */
@media (max-width: 768px) {
  :deep(.delivery-dialog-wrapper) {
    width: 90vw !important;
    margin: var(--admin-space-md);
  }

  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--admin-space-xs);
  }

  .label {
    width: auto;
  }

  .dialog-footer {
    flex-direction: column;
  }

  .dialog-footer .el-button {
    width: 100%;
  }
}
</style>
